#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e9;
int t;
ll n,a,b,d;
int main() {
    cin >> t;
    while(t--) {
        cin >> n >> a;
        b = a + n - 1;
        int L = 1, R = n, ans1 = N;
        // 保证 s1 - s2 > 0的i的最小
        while(L<=R) {
            int mid = (L+R) / 2;
            int midx = mid+a-1;
            ll s1=(a+midx)*mid/2, s2 = (midx+1+b)*(n-mid)/2;
            if(s1 - s2 >= 0) {
                ans1 = min(ans1, mid);
                R = mid-1;
            } else L = mid+1;
        }
        int midx = ans1 + a - 1;
        ll w1 = abs((a+midx)*ans1/2 - (midx+1+b)*(n-ans1)/2);
        // 保证 s2 - s1 >0 的i的最大值
        L = 1, R = n;
        int ans2 = 0;
        while(L<=R) {
            int mid = (L+R) / 2;
            int midx = mid+a-1;
            ll s1=(a+midx)*mid/2, s2 = (midx+1+b)*(n-mid)/2;
            if(s2 - s1 >= 0) {
                ans2 = max(ans2, mid);
                L = mid+1;
            } else R = mid-1;
        }
        midx = ans2 + a - 1;
        ll w2 = abs((a+midx)*ans2/2 - (midx+1+b)*(n-ans2)/2);
        cout << min(w1,w2)<<endl;
    }
    return 0;
}